package cn.doctorpda.conference.localstore;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import cn.doctorpda.conference.MyApplication;
import cn.doctorpda.conference.bean.User;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.utils.SPUtils;

/**
 * Created by huangjinlong on 2016/5/20.
 */
public class UserInfo {
    public static void saveUserInfo(User user) {
        SharedPreferences sp = MyApplication.getInstance().getSharedPreferences(AppConfig.SHARED_FILE_USER,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("nickname", user.getNICKNAME());
        editor.putString("username", user.getNAME());
        editor.putString("avatar", user.getAVATAR());
        editor.putString("mobile", user.getMOBILE());
        editor.putString("id", user.getID());
        editor.putString("lastlogin", user.getLAST_LOGIN_AT());
        editor.putString("createat", user.getCREATE_AT());
        editor.commit();
    }

    public static void clearUser() {
        SharedPreferences sp = MyApplication.getInstance().getSharedPreferences(AppConfig.SHARED_FILE_USER,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static User getUserInfo() {
        if (TextUtils.isEmpty(ClientInfo.getUserAccessToken(MyApplication.getInstance()))){
            return null;
        }
        SharedPreferences sp = MyApplication.getInstance().getSharedPreferences(AppConfig.SHARED_FILE_USER,
                Context.MODE_PRIVATE);
        User user = new User();
        user.setNICKNAME(sp.getString("nickname", ""));
        user.setNAME(sp.getString("username", ""));
        user.setAVATAR(sp.getString("avatar", ""));
        user.setMOBILE(sp.getString("mobile", ""));
        user.setID(sp.getString("id", ""));
        user.setLAST_LOGIN_AT(sp.getString("lastlogin", ""));
        user.setCREATE_AT(sp.getString("createat", ""));
        return user;
    }
}
